package me.xplabs.fight.layer 
{
	import com.greensock.TweenMax;
	import flash.display.Sprite;
	import me.xplabs.battle.buff.Buff;
	import me.xplabs.engine.Animation;
	import me.xplabs.gemo.Point2D;
	import me.xplabs.Globals;
	import me.xplabs.interfaces.IObjectMatrix;
	import me.xplabs.magic.LongDistanceMagic;
	import me.xplabs.magic.MagicAnimate;
	import me.xplabs.model.msg.MsgAddEffect;
	import me.xplabs.model.skill.MSkillInfo;
	import me.xplabs.utils.SimpleObjPool;
	
	/**
	 * http://www.xplabs.me/
	 * @author xiaohan
	 */
	public class MagicLayer extends Sprite 
	{ 
		private var _magics:Vector.<MagicAnimate>;
		private var _magicPool:SimpleObjPool;
		private var _animateUpdateKey:int;
		private var _effects:Vector.<Animation>;
		private var _effectPool:SimpleObjPool;
		
		public function MagicLayer() 
		{
			_magicPool = new SimpleObjPool(MagicAnimate, 1, 1);
			_magics = new Vector.<MagicAnimate>();
			_effectPool = new SimpleObjPool(Animation, 1, 1);
			_effects = new Vector.<Animation>();
		}
	
		/**
		 * 添加技能特效
		 * @param	mSkillInfo
		 */
		public function addMagic(p_skillInfo:MSkillInfo):void 
		{
			var tempMagic:MagicAnimate = _magicPool.getObj();
			//var tempMagic:MagicAnimate = new MagicAnimate();
			tempMagic.objMatrix = IObjectMatrix(p_skillInfo);
			var resBitmapData:* = Globals.lib.getResObj(Globals.domain.magic +"r" + p_skillInfo.hitId + ".i1");
			if (resBitmapData)
			{
				tempMagic.addBitmap(resBitmapData);
				var len:int = resBitmapData.length;
				for (var i:int = 1; i < len; i++) 
				{
					tempMagic.addBitmap(Globals.lib.getResObj(Globals.domain.magic +"r" + p_skillInfo.hitId + ".i" + (i + 1)));
				}
			}
			_magics.push(tempMagic);
			addChild(tempMagic);
			tempMagic.play();
		}
		
		/*public function inputSort(p:MagicAnimate):int
		{
			var len:int = _magics.length;
			for (var i:int = 0; i < len; i++) 
			{
				if(p.y
			}
		}*/
		public function delMagic(p_soleId:int):void
		{
			var len:int = _magics.length;
			for (var i:int = 0; i < len; i++) 
			{
				if (_magics[i].objMatrix.soleId == p_soleId)
				{
					//_magics[i].update();
					//Globals.timeManager.delayFrame(function():void
					//{
						//if (i > _magics.length - 1) return;
						_magics[i].dispose();
						if (_magics[i].parent) _magics[i].parent.removeChild(_magics[i]);
						_magicPool.dispose(_magics.splice(i, 1)[0]);
					//});
					return;
				}
			}
			//trace("没有移除吗");
		}
		
		public function addEffect(msg:MsgAddEffect):void
		{
			//var tempMagic:Animation = _magicPool.getObj();
			var tempMagic:Animation = new Animation();
			tempMagic.curPosX = msg.x;
			tempMagic.curPosY = msg.y;
			tempMagic.object = Globals.soleId;
			//tempMagic.objMatrix = IObjectMatrix(p_skillInfo);
			var resBitmapData:* = Globals.lib.getResObj(Globals.domain.magic +"r" + msg.resId+ ".i1");
			if (resBitmapData)
			{
				tempMagic.addBitmap(resBitmapData);
				var len:int = resBitmapData.length;
				for (var i:int = 1; i < len; i++) 
				{
					tempMagic.addBitmap(Globals.lib.getResObj(Globals.domain.magic +"r" + msg.resId + ".i" + (i + 1)));
				}
			}
			tempMagic.repeat = false;
			_effects.push(tempMagic);
			addChild(tempMagic);
			tempMagic.play(40);
			tempMagic.playOver = effectPlayOverHandler;
		}
		
		private function effectPlayOverHandler(p:Animation):void 
		{
			delEffect(int(p.object));
		}
		public function delEffect(p_soleId:int):void
		{
			var len:int = _effects.length;
			for (var i:int = 0; i < len; i++) 
			{
				if (int(_effects[i].object) == p_soleId)
				{
					//_magics[i].update();
					//Globals.timeManager.delayFrame(function():void
					//{
						//if (i > _effects.length - 1) return;
						_effects[i].dispose();
						if (_effects[i].parent) _effects[i].parent.removeChild(_effects[i]);
						_effectPool.dispose(_effects.splice(i, 1)[0]);
						Globals.delSoleId(p_soleId);
					//});
					return;
				}
			}
		}
		

		/** 添加buff特效到特效层*/
		public function addBuff(p_buff:Buff):void
		{
			var tempMagic:MagicAnimate = _magicPool.getObj();
			//var tempMagic:MagicAnimate = new MagicAnimate();
			tempMagic.objMatrix = IObjectMatrix(p_buff);
			var resBitmapData:* = Globals.lib.getResObj(Globals.domain.magic +"r" + p_buff.magic + ".i1");
			if (resBitmapData)
			{
				tempMagic.addBitmap(resBitmapData);
				var len:int = resBitmapData.length;
				for (var i:int = 1; i < len; i++) 
				{
					tempMagic.addBitmap(Globals.lib.getResObj(Globals.domain.magic +"r" + p_buff.magic + ".i" + (i + 1)));
				}
			}
			addChild(tempMagic);
			_magics.push(tempMagic);
			tempMagic.play(60);
			
		}
		
		public function update():void
		{
			var len:int = _magics.length;
			for (var i:int = 0; i < len; i++) 
			{
				if (i > _magics.length - 1) break;
				_magics[i].update();
			}
			len = _effects.length;
			for (i = 0; i < len; i++) 
			{
				if (i > _effects.length - 1) break;
				_effects[i].update();
			}
		}
		
		public function dispose():void 
		{
			
		}
	}
}